/*!
 * uni-mini-router v0.1.6
 * 2023/12/21 13:42:18 weisheng
 */
import {
  inject as e,
  reactive as r,
  watch as t,
  unref as n,
  shallowRef as o,
} from "vue";
function a(e) {
  return (
    (a =
      "function" == typeof Symbol && "symbol" == typeof Symbol.iterator
        ? function (e) {
            return typeof e;
          }
        : function (e) {
            return e &&
              "function" == typeof Symbol &&
              e.constructor === Symbol &&
              e !== Symbol.prototype
              ? "symbol"
              : typeof e;
          }),
    a(e)
  );
}
function i(e) {
  var r = (function (e, r) {
    if ("object" !== a(e) || null === e) return e;
    var t = e[Symbol.toPrimitive];
    if (void 0 !== t) {
      var n = t.call(e, r || "default");
      if ("object" !== a(n)) return n;
      throw new TypeError("@@toPrimitive must return a primitive value.");
    }
    return ("string" === r ? String : Number)(e);
  })(e, "string");
  return "symbol" === a(r) ? r : r + "";
}
function u(e, r, t) {
  return (
    (r = i(r)) in e
      ? Object.defineProperty(e, r, {
          value: t,
          enumerable: !0,
          configurable: !0,
          writable: !0,
        })
      : (e[r] = t),
    e
  );
}
var c,
  l = Symbol("__ROUTER__"),
  f = Symbol("__ROUTE__");
!(function (e) {
  (e.push = "navigateTo"),
    (e.replace = "redirectTo"),
    (e.replaceAll = "reLaunch"),
    (e.pushTab = "switchTab"),
    (e.back = "navigateBack");
})(c || (c = {}));
var s = ["navigateTo", "redirectTo", "reLaunch", "switchTab", "navigateBack"];
function p(e) {
  var r = {},
    t = e.split("?"),
    n = "",
    o = [];
  t.length > 1 && (n = t[1]), (o = n.split("&"));
  for (var a = 0; o.length > a; a++)
    2 === o[a].split("=").length &&
      (r[o[a].split("=")[0]] = o[a].split("=")[1]);
  return r;
}
function v(e, r, t) {
  return e.replace(RegExp(r, "g"), t);
}
function h(e) {
  return (
    (e = v(e, "//", "/")),
    (e = v(e, "https:/", "https://")),
    (e = v(e, "http:/", "http://"))
  );
}
function b(e) {
  return null == e || 0 === Object.keys(e).length;
}
function g(e, r) {
  var t = Object.keys(e);
  if (Object.getOwnPropertySymbols) {
    var n = Object.getOwnPropertySymbols(e);
    r &&
      (n = n.filter(function (r) {
        return Object.getOwnPropertyDescriptor(e, r).enumerable;
      })),
      t.push.apply(t, n);
  }
  return t;
}
function y(e) {
  for (var r = 1; arguments.length > r; r++) {
    var t = null != arguments[r] ? arguments[r] : {};
    r % 2
      ? g(Object(t), !0).forEach(function (r) {
          u(e, r, t[r]);
        })
      : Object.getOwnPropertyDescriptors
        ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t))
        : g(Object(t)).forEach(function (r) {
            Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
          });
  }
  return e;
}
var d = {
  navigateTo: uni.navigateTo,
  redirectTo: uni.redirectTo,
  reLaunch: uni.reLaunch,
  switchTab: uni.switchTab,
  navigateBack: uni.navigateBack,
};
function m(e, r, t) {
  var n = O(e, r);
	((function (e, r) {
		var t = "";
		"string" == typeof e
			? ((t = h("/".concat(e.split("?")[0]))),
				r.routes.forEach(function (r) {
					r.path === t && (r.query = p(e));
				}))
			: e.name
				? r.routes.forEach(function (r) {
						r.name === e.name && (r.params = e.params);
					})
				: e.path &&
					((t = h("/".concat(e.path.split("?")[0]))),
					r.routes.find(function (r) {
						r.path === t && (r.query = y(y({}, p(e.path)), e.query || {}));
					}));
	})(e, r),
	t);
  switch (t) {
    case "push":
      d.navigateTo({ url: n });
      break;
    case "replace":
      d.redirectTo({ url: n });
      break;
    case "pushTab":
      d.switchTab({ url: n });
      break;
    case "replaceAll":
      d.reLaunch({ url: n });
      break;
    default:
      throw Error("无效的路由类型，请确保提供正确的路由类型");
  }
}
function O(e, r) {
  var t = "",
    n = {};
  if ("string" == typeof e) t = e;
  else {
    if (e.name) {
      var o = r.routes.find(function (r) {
        return r.name === e.name;
      });
      if (!o || !o.path)
        throw Error(
          "您正在尝试访问的路由未在路由表中定义。请检查您的路由配置。",
        );
      (t = o.path), (n = e.params);
    } else
      e.path &&
        ((t = h("/".concat(e.path.split("?")[0]))),
        (n = y(y({}, p(e.path)), e.query || {})));
    n &&
      (t = (function (e, r) {
        for (var t in r)
          e.indexOf("?") > -1
            ? (e += "&".concat(t, "=").concat(r[t]))
            : (e += "?".concat(t, "=").concat(r[t]));
        return e;
      })(
        t,
        (n = (function (e) {
          var r = {};
          if (e)
            for (var t in e) {
              var n = e[t];
              void 0 === n && (n = ""), (r[t] = n);
            }
          return r;
        })(n)),
      ));
  }
  return t;
}
function k(e) {
  var r,
    t = (r = getCurrentPages()).length > 0 ? r[r.length - 1] : void 0;
  if (t && t.route && e.routes) {
    var n = P("/".concat(t.route), e);
    return (
      t.$page &&
        ((n.fullPath = t.$page.fullPath ? t.$page.fullPath : ""),
        (n.query = t.$page.fullPath ? p(t.$page.fullPath) : {}),
        (n.params = t.$page.fullPath ? p(t.$page.fullPath) : {})),
      n
    );
  }
}
function P(e, r) {
  e = h(e.split("?")[0]);
  var t = r.routes.find(function (r) {
    return r.path === e || r.aliasPath === e;
  });
  return JSON.parse(JSON.stringify(t));
}
function T(e, r, t) {
  e.guardHooks[r] = [t];
}
var w = {
  navigateTo: uni.navigateTo,
  redirectTo: uni.redirectTo,
  reLaunch: uni.reLaunch,
  switchTab: uni.switchTab,
  navigateBack: uni.navigateBack,
};
function j(e) {
  s.forEach(function (r) {
    d[r] = function (t) {
      if ("navigateBack" === r) w[r](t);
      else if (e.guardHooks.beforeHooks && e.guardHooks.beforeHooks[0]) {
        var n = P(t.url, e);
        (function (e, r, t) {
          return new Promise(function (n, o) {
            var i = function e(r) {
                (e._called = !0),
                  !1 === r ? o({}) : n(void 0 === r || !0 === r || r);
              },
              u = e.call(void 0, r, t, i),
              c = Promise.resolve(u);
            if ((3 > e.length && (c = c.then(i)), e.length > 2)) {
              var l = 'The "next" callback was never called inside of '
                .concat(e.name ? '"' + e.name + '"' : "", ":\n")
                .concat(
                  "" + e,
                  '\n. If you are returning a value instead of calling "next", make sure to remove the "next" parameter from your function.',
                );
              if (null !== u && "object" === a(u) && "then" in u)
                c = c.then(function (e) {
                  return i._called
                    ? e
                    : (console.warn(l),
                      Promise.reject(Error("Invalid navigation guard")));
                });
              else if (!i._called)
                return (
                  console.warn(l), void o(Error("Invalid navigation guard"))
                );
            }
            c.catch(function (e) {
              return o(e);
            });
          });
        })(e.guardHooks.beforeHooks[0], n, e.route.value)
          .then(function (n) {
            if (!0 === n) w[r](t);
            else if ("string" == typeof n) {
              var o = O(n, e);
              w[r]({ url: o });
            } else if ("back" === n.navType) w.navigateBack(n);
            else {
              var a = O(n, e);
              w[n.navType ? c[n.navType] : r]({ url: a });
            }
          })
          .catch(function (e) {
            throw e;
          });
      } else w[r](t);
    };
  });
}
function H() {
  var r = e(l);
  if (r) return r;
  throw Error(
    'useRouter 只可以在 Vue 上下文中使用，请确保你已经正确地注册了 "uni-mini-router" 并且当前正处于 Vue 上下文中',
  );
}
function E() {
  var n = e(f);
  if (n) {
    var o = r(n.value);
    return (
      t(n, function (e) {
        Object.assign(o, e);
      }),
      o
    );
  }
  throw Error(
    'useRoute 只可以在 Vue 上下文中使用，请确保你已经正确地注册了 "uni-mini-router" 并且当前正处于 Vue 上下文中',
  );
}
function S(e, r) {
  var t = Object.keys(e);
  if (Object.getOwnPropertySymbols) {
    var n = Object.getOwnPropertySymbols(e);
    r &&
      (n = n.filter(function (r) {
        return Object.getOwnPropertyDescriptor(e, r).enumerable;
      })),
      t.push.apply(t, n);
  }
  return t;
}
function $(e) {
  for (var r = 1; arguments.length > r; r++) {
    var t = null != arguments[r] ? arguments[r] : {};
    r % 2
      ? S(Object(t), !0).forEach(function (r) {
          u(e, r, t[r]);
        })
      : Object.getOwnPropertyDescriptors
        ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t))
        : S(Object(t)).forEach(function (r) {
            Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r));
          });
  }
  return e;
}
function _(e) {
  var r = {
    routes: e.routes,
    guardHooks: { beforeHooks: null, afterHooks: null },
    push: function (e) {
      return m(e, this, "push");
    },
    replace: function (e) {
      return m(e, this, "replace");
    },
    replaceAll: function (e) {
      return m(e, this, "replaceAll");
    },
    pushTab: function (e) {
      return m(e, this, "pushTab");
    },
    back: function (e) {
      return uni.navigateBack(e);
    },
    beforeEach: function (e) {
      T(r, "beforeHooks", e);
    },
    afterEach: function (e) {
      T(r, "afterHooks", e);
    },
    install: function (e) {
      var r = this,
        t = this;
      e.provide(l, this),
        e.provide(f, this.route),
        j(t),
        e.mixin({
          beforeCreate: function () {
            if (
              "page" === this.$mpType &&
              t.guardHooks.afterHooks &&
              t.guardHooks.afterHooks[0]
            ) {
              var e = t.route.value,
                r = k(t);
              t.guardHooks.afterHooks[0].call(null, r, e);
            }
          },
          onLoad: function (e) {
            !b(e) &&
              b(t.route.value.query) &&
              b(t.route.value.params) &&
              (t.route.value = $($({}, t.route.value), {}, { query: e }));
          },
          onShow: function () {
            "page" === this.$mpType &&
              (function (e) {
                e.route.value = k(e);
              })(t);
          },
        }),
        Object.defineProperty(e.config.globalProperties, "$Router", {
          get: function () {
            return t;
          },
        }),
        Object.defineProperty(e.config.globalProperties, "$Route", {
          enumerable: !0,
          get: function () {
            return n(r.route);
          },
        });
    },
    route: o({ path: "/" }),
  };
  return r;
}
export {
  s as NavMethod,
  c as NavTypeEnum,
  _ as createRouter,
  E as useRoute,
  H as useRouter,
};
